// Copyright (c) 2019 Presto Labs Pte. Ltd.
// Author: donggu

#include "coin2/base/conversion.h"

#include <doctest.h>
#include <fast_double_parser.h>
#include <nanobench.h>

/*

|  ns/op |            op/s | err% |  ins/op |  bra/op | miss% | total | benchmark
|-------:|----------------:|-----:|--------:|--------:|------:|------:|:----------
|  39.03 |   25,623,130.61 | 0.4% |  636.02 |  135.00 |  0.0% |  0.00 | `absl::from_chars`
|   9.62 |  103,992,979.38 | 0.0% |  157.01 |   27.00 |  0.0% |  0.00 | `fast_double_parser::stod`
|   8.55 |  116,943,820.22 | 0.0% |  155.01 |   17.00 |  0.0% |  0.00 | `unsafe_stod`
*/

DOCTEST_TEST_CASE("bench string to double") {
  std::string a = "21424.124124e-3";
  double x;
  ankerl::nanobench::Bench()
      .run("absl::from_chars", [&] { x = absl_from_chars<double>(a); })
      .doNotOptimizeAway(x);
  ankerl::nanobench::Bench()
      .run("fast_double_parser::stod", [&] { x = fast_double_parser::stod(a.c_str()); })
      .doNotOptimizeAway(x);
  // this function is removed
  //   ankerl::nanobench::Bench()
  //       .run("unsafe_stod", [&] { x = unsafe_stod(a.c_str()); })
  //       .doNotOptimizeAway(x);
}

/*
|  ns/op |            op/s |    err% |   ins/op |  bra/op |   miss% |     total | benchmark
|-------:|----------------:|--------:|---------:|--------:|--------:|----------:|:----------
|  17.19 |   58,173,356.60 |    0.1% |   277.01 |   84.00 |    0.0% |      0.00 | `std::from_chars`
|  21.99 |   45,470,932.85 |    0.0% |   411.02 |  118.00 |    0.0% |      0.00 | `stoull`
*/

DOCTEST_TEST_CASE("bench string to int64_t") {
  std::string a = "8127244891274891";
  int64_t x = 0;
  // ankerl::nanobench::Bench()
  //     .run("std::from_chars", [&] { std::from_chars(a.c_str(), a.c_str() + a.size(), x); })
  //     .doNotOptimizeAway(x);
  ankerl::nanobench::Bench().run("stoull", [&] { x = stoull(a); }).doNotOptimizeAway(x);
  ankerl::nanobench::Bench().run("std::stoull", [&] { x = std::stoull(a); }).doNotOptimizeAway(x);
}
